package com.han.hotelplat.controller.hotelmanager;

import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.JarOutputStream;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.crypto.dsig.spec.HMACParameterSpec;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.han.hotelplat.cache.redis.IRedisCache;
import com.han.hotelplat.cache.redis.RedisCache;
import com.han.hotelplat.comm.Constant;
import com.han.hotelplat.comm.ResponseWrapTo;
import com.han.hotelplat.comm.ResponseWrapTo.CodeType;
import com.han.hotelplat.comm.utils.AreaHelp;
import com.han.hotelplat.comm.utils.LogUtil;
import com.han.hotelplat.comm.utils.MD5;
import com.han.hotelplat.comm.utils.MsgHelp;
import com.han.hotelplat.comm.utils.NumberUtil;
import com.han.hotelplat.comm.utils.RequestUtil;
import com.han.hotelplat.comm.utils.StrUtil;
import com.han.hotelplat.comm.utils.UUIDUtils;
import com.han.hotelplat.manager.LogManager;
import com.han.hotelplat.pojo.clientversion.ClientVersion;
import com.han.hotelplat.pojo.employee.Employee;
import com.han.hotelplat.pojo.hhm.HotelHmanager;
import com.han.hotelplat.pojo.hotel.Hotel;
import com.han.hotelplat.pojo.hotelmanager.Hotelmanager;
import com.han.hotelplat.pojo.question.Question;
import com.han.hotelplat.pojo.wallet.Wallet;
import com.han.hotelplat.service.clientversion.ClientversionService;
import com.han.hotelplat.service.hhm.HotelHmanagerService;
import com.han.hotelplat.service.hotel.HotelService;
import com.han.hotelplat.service.hotelmanager.HotelmanagerService;
import com.han.hotelplat.service.hotelroom.HotelRoomService;
import com.han.hotelplat.service.question.QuestionService;
import com.han.hotelplat.service.wallet.WalletService;

@Controller
@RequestMapping("/hotelmanager/*")
public class HotelmanagerController {
	@Autowired
	private HotelmanagerService hotelmanagerService;
	@Autowired
	private HotelService hotelService;
	@Autowired
	private WalletService walletService;
	
	@Autowired
	private IRedisCache redisCache;
	
	@Autowired
	private ClientversionService clientversionService;
	
	@Autowired
	private QuestionService questionService;
	
	@Autowired
	private HotelHmanagerService hhmService;
	
	@Autowired
	private HotelRoomService hotelRoomService;
	
	/**
	 * 酒店切换
		 * @description	
		 * @author  han1088
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "changeHotel", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String changeHotel(Integer hotelId){
		Hotelmanager hotelmanager =  (Hotelmanager) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
		List<HotelHmanager> re = hhmService.getBindHotelManager(hotelId, hotelmanager.getUserid(), 1);
		if(re.size() == 0){
			return new ResponseWrapTo(-2, "还未绑定该酒店或者已经解绑", null).toJson();
		}
		//更新会话数据跟数据库数据
		hotelmanager.setHotelid(re.get(0).getHotelid());
		hotelmanagerService.update(hotelmanager);
		RequestUtil.setSessionAttribute(Constant.SESSION_LOGIN_USER, hotelmanager);
		
		return new ResponseWrapTo(CodeType.success, "数据切换成功", null).toJson();
	}
	
	/**
	 * 获取已经绑定的酒店
		 * @description	
		 * @author  han1088
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getMyBindHotel", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String getMyBindHotel(){
		Hotelmanager hotelmanager =  (Hotelmanager) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
		if(hotelmanager == null){
			return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
		}
		List<HotelHmanager> re = hhmService.getBindHotelManager(null, hotelmanager.getUserid(), 1);
		if(re.size() == 0){
			return new ResponseWrapTo(-2, "还未绑定酒店", null).toJson();
		}
		JSONArray array = new JSONArray();
		for(HotelHmanager hhm : re){
			JSONObject json = new JSONObject();
			json.put("hotelId", hhm.getHotelid());
			json.put("hotelName", hhm.getHotelname());
			array.add(json);
		}
		return new ResponseWrapTo(CodeType.success, "数据获取成功", array).toJson();
	}
	
	@ResponseBody
	@RequestMapping(value = "changeTelAuthVc", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String changeTelAuthVc(String vcode){
		Hotelmanager hotelmanager =  (Hotelmanager) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
		if(hotelmanager == null){
			return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
		}
		
		String tel = (String) RequestUtil.getSessionAttribute("changetel");
		if(tel == null){
			return new ResponseWrapTo(-3, "验证码失效请重新获取", null).toJson();
		}
		
		if(tel.equals(hotelmanager.getTel())){
			return new ResponseWrapTo(-1, "你当前已绑定该手机号", null).toJson();
		}
		
		Hotelmanager oldhotelmanager = hotelmanagerService.findByTel(tel);
		if(oldhotelmanager != null){
			return new ResponseWrapTo(-2, "该手机号已被其他账号绑定，请先解绑该账号，在进行绑定", null).toJson();
		}
		
		String cashVcode = redisCache.get(Constant.CACHE_PHONE_VCODE_CHANGETEL+tel);
		if(!cashVcode.equals(vcode)){
			return new ResponseWrapTo(-3, "验证码错误", null).toJson();
		}
		//重置手机号码
		hotelmanager.setTel(tel);
		hotelmanagerService.update(hotelmanager);
		RequestUtil.setSessionAttribute(Constant.SESSION_LOGIN_USER, oldhotelmanager);
		//清除验证码
		redisCache.delete(Constant.CACHE_PHONE_VCODE_CHANGETEL+tel);
		
		return new ResponseWrapTo(0, "手机号码替换成功", null).toJson();
	}
	
	@ResponseBody
	@RequestMapping(value = "changeTelSendSMS", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String changeTelSendSMS(String tel){
		Hotelmanager hotelmanager =  (Hotelmanager) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
		if(hotelmanager == null){
			return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
		}
		if(tel.equals(hotelmanager.getTel())){
			return new ResponseWrapTo(-1, "你当前已绑定该手机号", null).toJson();
		}
		Hotelmanager oldhotelmanager = hotelmanagerService.findByTel(tel);
		if(oldhotelmanager != null){
			return new ResponseWrapTo(-2, "该手机号已被其他账号绑定，请先解绑该账号，在进行绑定", null).toJson();
		}
		
		int vcode = NumberUtil.getRandomNumber(1000, 9999);
		redisCache.set(Constant.CACHE_PHONE_VCODE_CHANGETEL+tel, vcode);
		RequestUtil.setSessionAttribute("changetel", tel);
		try {
			MsgHelp.sendCheckCode(tel, vcode+"",Constant.USER_TYPE_HOTELMANAGER);
		} catch (ClientException e) {
			e.printStackTrace();
			return new ResponseWrapTo(CodeType.fail,"验证码发送异常",null).toJson();
		}
		return new ResponseWrapTo(CodeType.success,"验证码发送成功",null).toJson();
	}
	
	/**
	 * 
		 * @description	客服中心意见反馈
		 * @author  han1088
		 * @param q
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "submitQuestion", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String submitQuestion(Question q){
		Hotelmanager em =  (Hotelmanager) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
		if(em == null){
			return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
		}
		q.setUsertype(Constant.USER_TYPE_HOTELMANAGER);
		q.setCreattime(new Date());
		q.setEndtime(new Date());
		q.setUserid(em.getUserid());
		q.setState(1);//待解决
		questionService.save(q);
		return new ResponseWrapTo(CodeType.success, "反馈已收到，请耐心等待回复", null).toJson();
	}
	
	//修改管家信息
	@ResponseBody
	@RequestMapping(value = "updateHotel", produces = "application/json;charset=UTF-8", method = { RequestMethod.POST,RequestMethod.GET })
	public String updateHotel(String data){
		JSONObject jobj = JSONObject.parseObject(data);
		JSONObject hotelmanagerJSObject = jobj.getJSONObject("hotelmanager");
		//JSONObject hotelJSObject = jobj.getJSONObject("hotel");
		hotelmanagerJSObject.remove("hotel");
		Hotelmanager hotelmanager = JSONObject.parseObject(hotelmanagerJSObject.toJSONString(), Hotelmanager.class);
		//Hotel hotel = JSONObject.parseObject(hotelJSObject.toJSONString(), Hotel.class);
		
		Hotelmanager oldhotelmanager = (Hotelmanager) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
		
		if(oldhotelmanager == null){
			return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
		}
		
		//Hotel oldHotel = null;
		/*if(oldhotelmanager.getHotelid() != null){
			oldHotel = hotelService.getHotelById(oldhotelmanager.getHotelid());
		}*/
		
		if(oldhotelmanager.getId().intValue() != hotelmanager.getId().intValue()){
			return new ResponseWrapTo(CodeType.fail, "资料数据有误", null).toJson();
		}
		
		/*if(oldHotel != null && hotel.getId().intValue() != oldHotel.getId().intValue()){
			return new ResponseWrapTo(CodeType.fail, "资料数据有误,酒店ID不一致", null).toJson();
		}*/
		
		if(hotelmanager.getId() == null){
			return new ResponseWrapTo(CodeType.fail, "资料不全不能更新", null).toJson();
		}
		Map<String, Object> re = checkHmAttribute(hotelmanager);
		int code =  (int) re.get("code");
		String message = (String) re.get("message");
		if(code < 0){
			return new ResponseWrapTo(CodeType.fail, message, null).toJson();
		}
		oldhotelmanager.setRealname(hotelmanager.getRealname());
		oldhotelmanager.setSex(hotelmanager.getSex());
		oldhotelmanager.setTel(hotelmanager.getTel());
		oldhotelmanager.setPosition(hotelmanager.getPosition());
		oldhotelmanager.setIntroduceritel(hotelmanager.getIntroduceritel());
		oldhotelmanager.setState(Constant.USER_STATE_CREAT);
		oldhotelmanager.setFacepic(hotelmanager.getFacepic());
		/*Map<String, Object> result = checkHotelAttribute(hotel);
		code =  (int) result.get("code");
		message = (String) result.get("message");
		if(code < 0){
			return new ResponseWrapTo(CodeType.fail, message, null).toJson();
		}*/
		
		/*if(hotel.getId() == null && oldHotel == null){//新增酒店资料
			hotel.setState(Constant.HOTEL_STATE_WAITAUTH);
			hotel.setCreattime(new Date());
			hotel.setUserid(hotelmanager.getUserid());
			hotelService.saveHotel(hotel);
		}else{
			hotel.setUserid(hotelmanager.getUserid());
			hotel.setState(Constant.HOTEL_STATE_WAITAUTH);
			hotelService.update(hotel);
		}*/
		//oldhotelmanager.setHotelid(hotel.getId());
		hotelmanagerService.update(oldhotelmanager);
		return new ResponseWrapTo(CodeType.success, "资料提交成功请等待审核", oldhotelmanager).toJson();
	}
	
	private Map<String, Object> checkHotelAttribute(Hotel hotel) {
		int code = -1;
		String message = "";
		Map<String, Object> data = new HashMap<String, Object>();
		if(StrUtil.isNullOrEmpty(hotel.getFacepic())){
			message = "门头照不能为空"; 
		}
		if(hotel.getHoteltype() == null){
			message = "酒店类型不能为空"; 
		}
		if(hotel.getArea() == null){
			message = "所在地区不能为空"; 
		}
		if(StrUtil.isNullOrEmpty(hotel.getAddress())){
			message = "详细地址不能为空"; 
		}
		if(hotel.getRoomcount() == null){
			message = "房量不能为空"; 
		}
		if(!StrUtil.isNullOrEmpty(message)){
			data.put("code", code);
			data.put("message", message);
			return data;
		}
		data.put("code", 0);
		data.put("message", "资料齐全");
		return data;
	}

	private Map<String, Object> checkHmAttribute(Hotelmanager hotelmanager) {
		int code = -1;
		String message = "";
		Map<String, Object> data = new HashMap<String, Object>();
		if(StrUtil.isNullOrEmpty(hotelmanager.getRealname())){
			message = "姓名不能为空"; 
		}
		if(hotelmanager.getPosition() == null){
			message = "职位不能为空"; 
		}
		if(hotelmanager.getSex() == null){
			message = "性别不能为空"; 
		}
		if(StrUtil.isNullOrEmpty(hotelmanager.getTel())){
			message = "手机号不能为空"; 
		}
		if(!StrUtil.isNullOrEmpty(message)){
			data.put("code", code);
			data.put("message", message);
			return data;
		}
		data.put("code", 0);
		data.put("message", "资料齐全");
		return data;
	}

	@ResponseBody
	@RequestMapping(value = "getHoteInfo", produces = "application/json;charset=UTF-8", method = { RequestMethod.POST,RequestMethod.GET })
	public String getHoteInfo(){
		Hotelmanager oldhotelmanager = (Hotelmanager) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
		if(oldhotelmanager == null){
			return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
		}
		Hotelmanager newhotelmanager= hotelmanagerService.findByUserid(oldhotelmanager.getUserid());
		Hotel hotel = null;
		if(newhotelmanager.getHotelid() != null){
			hotel = hotelService.getHotelById(newhotelmanager.getHotelid());
		}
		
		if(hotel != null && hotel.getArea() != null){//酒店管家还未提交酒店资料
			hotel.setAreaModel(AreaHelp.getInstance().getAreaModels(hotel.getArea().toString()));
		}
		newhotelmanager.setHotel(hotel);
		RequestUtil.setSessionAttribute(Constant.SESSION_LOGIN_USER, newhotelmanager);
		String hmjs = JSONObject.toJSONString(newhotelmanager);
		JSONObject data = JSONObject.parseObject(hmjs);
		data.put("hotelmanager", newhotelmanager);
		data.put("hotel", hotel);
		return new ResponseWrapTo(CodeType.success, "数据获取成功", data).toJson();
	}
	
	@ResponseBody
	@RequestMapping(value = "quickLogin", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET })
	public String quickLogin(HttpServletRequest req,String token) {
		String userid = token.split("_")[0];
		String cacheToken = redisCache.get(Constant.SESSION_LOGIN_USER_TOKEN+userid).toString();
		if(!cacheToken.equals(token)){
			return new ResponseWrapTo(CodeType.fail,"登陆状态无效，请使用手机号码登陆",null).toJson();
		}
		Map result = hotelmanagerService.quickLogin(userid);
		int code = (int) result.get("code");
		Hotelmanager hotelmanager = null;
		if(code == 0){//登陆成功
			hotelmanager = (Hotelmanager) result.get("user");
			
			//给一个默认的酒店
			List<HotelHmanager> re = hhmService.getBindHotelManager(null, hotelmanager.getUserid(), 1);
			if(hotelmanager.getHotelid() == null){//此前未绑定过酒店
				if(re.size()>0){
					hotelmanager.setHotelid(re.get(0).getHotelid());
				}
			}
			redisCache.set(Constant.SESSION_LOGIN_USER_TOKEN+hotelmanager.getUserid(), token);
			RequestUtil.setSessionAttribute(Constant.SESSION_LOGIN_USER, hotelmanager);
			hotelmanager.setToken(token);
			hotelmanager.setLastlogintime(new Date());
			hotelmanagerService.update(hotelmanager);
			JSONObject js = (JSONObject) JSONObject.toJSON(hotelmanager);
			js.put("hotelmanager", hotelmanager);
			return new ResponseWrapTo(CodeType.success,"登陆成功",js).toJson();
		}
		return new ResponseWrapTo(CodeType.fail,"登陆失败，请使用手机号码登陆",null).toJson();
	}
	
	
	/**
	 * 
		 * @description	
		 * @author  han1088
		 * @param token
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "initApp", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET })
	public String initApp(String token, Integer type, String pushkey) {
		LogUtil.info("初始化参数：token="+token+",type="+type+",pushkey="+pushkey);
		Map<String, Object> data = new HashMap<String, Object>();
		ClientVersion c = clientversionService.getNowVersion(Constant.APP_TYPE_HOTEL,type);//查询酒店端最新版本
		data.put("appversion", c);
		if(token == null || token.isEmpty()){
			data.put("user", null);
			return new ResponseWrapTo(0,"请使用手机号码登陆",data).toJson();
		}
		String userid = token.split("_")[0];
		String cacheToken = redisCache.get(Constant.SESSION_LOGIN_USER_TOKEN+userid);
		if(cacheToken == null || !cacheToken.equals(token)){
			data.put("user", null);
			LogUtil.info("初始化登陆失败userid="+userid 
					+";cacheToken="+cacheToken
					+";token="+token);
			return new ResponseWrapTo(0,"请使用手机号码登陆",data).toJson();
		}
		Map result = hotelmanagerService.quickLogin(userid);
		int code = (int) result.get("code");
		Hotelmanager hotelmanager = null;
		if(code == 0){//登陆成功
			hotelmanager = (Hotelmanager) result.get("user");
			hotelmanager.setToken(token);
			hotelmanager.setPushkey(pushkey);
			hotelmanager.setClientype(type);
			hotelmanager.setLastlogintime(new Date());
			hotelmanagerService.update(hotelmanager);
			Hotel h = hotelmanager.getHotel();
			if(h != null && h.getArea()!= null){
				h.setAreaModel(AreaHelp.getInstance().getAreaModels(h.getArea().toString()));
			}
			
			//给一个默认的酒店
			List<HotelHmanager> re = hhmService.getBindHotelManager(null, hotelmanager.getUserid(), 1);
			if(hotelmanager.getHotelid() == null){//此前未绑定过酒店
				if(re.size()>0){
					hotelmanager.setHotelid(re.get(0).getHotelid());
				}
			}
			
			RequestUtil.setSessionAttribute(Constant.SESSION_LOGIN_USER, hotelmanager);
			//data.put("user", hotelmanager);
			String user = JSONObject.toJSONString(hotelmanager);
			JSONObject userJS = JSONObject.parseObject(user);
			userJS.put("hotelmanager", hotelmanager);
			data.put("user", userJS);
			return new ResponseWrapTo(CodeType.success,"登陆成功",data).toJson();
		}
		return new ResponseWrapTo(0,"请使用手机号码登陆",null).toJson();
	}
	
	
	/**
	 * 
		 * @description	用于手机号+验证码登陆
		 * @author  han1088
		 * @param phone 手机号
		 * @param code 验证码
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "login", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET })
	public String login(HttpServletRequest req,String phone, String vcode, String pushkey, Integer osType) {
		LogUtil.info("初始化参数：phone="+phone+",vcode="+vcode+",pushkey="+pushkey+",osType="+osType);
		Map result = hotelmanagerService.login(phone, vcode);
		int code = (int) result.get("code");
		Hotelmanager hotelmanager = null;
		if(code == 0){//登陆成功
			hotelmanager = (Hotelmanager) result.get("user");
			Hotel h = hotelmanager.getHotel();
			if(h == null){
				hotelmanager.setState(Constant.HOTEL_STATE_WAITAUTH);
			}else{
				hotelmanager.setState(hotelmanager.getHotel().getState());
			}
			String token = hotelmanager.getUserid()+"_"+MD5.toMD5(UUIDUtils.getUUID()+"");
			System.out.println(token);
			//给一个默认的酒店
			List<HotelHmanager> re = hhmService.getBindHotelManager(null, hotelmanager.getUserid(), 1);
			if(hotelmanager.getHotelid() == null){//此前未绑定过酒店
				if(re.size()>0){
					hotelmanager.setHotelid(re.get(0).getHotelid());
				}
			}
			
			redisCache.set(Constant.SESSION_LOGIN_USER_TOKEN+hotelmanager.getUserid(), token);
			RequestUtil.setSessionAttribute(Constant.SESSION_LOGIN_USER, hotelmanager);
			hotelmanager.setToken(token);
			hotelmanager.setClientype(osType);
			hotelmanager.setLastlogintime(new Date());
			hotelmanagerService.update(hotelmanager);
			JSONObject js = (JSONObject) JSONObject.toJSON(hotelmanager);
			js.put("hotelmanager", hotelmanager);
			return new ResponseWrapTo(CodeType.success,"登陆成功",js).toJson();
		}
		return new ResponseWrapTo(CodeType.fail,"验证码错误",null).toJson();
	}
	
	/**
	 * 
		 * @description	用于手机号+验证码登陆
		 * @author  han1088
		 * @param phone 手机号
		 * @param code 验证码
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "setInfo", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET })
	public String setInfo() {
		Hotelmanager hm =  (Hotelmanager) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
		Map<String, Object> data = new HashMap<String, Object>();
		if(hm == null){
			return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
		}
		if(hm.getHotelid() == null || hm.getHotelid().intValue() == -1){//酒店管家还没提供酒店资料
			data.put("isSetPayPassword", 2);//未设置支付密码
		}else{
			Wallet wallet = walletService.getWallet(hm.getHotelid().toString());
			if(wallet.getPaypassword() == null){//还未激活过
				data.put("isSetPayPassword", 2);//未设置支付密码
			}else{
				data.put("isSetPayPassword", 1);//已设置支付密码
			}
		}
		/*if(loginPassword == null){//还未激活过
			data.put("isSetLoginPassword", 2);//未设置支付密码
		}else{
			data.put("isSetLoginPassword", 1);//已设置支付密码
		}*/
		
		return new ResponseWrapTo(CodeType.success,"",data).toJson();
	}
	
	/**
	 * 
		 * @description	发送短信验证码
		 * @author  han1088
		 * @param phone 手机号
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "sendvcode", produces = "application/json;charset=UTF-8", method = { RequestMethod.POST,RequestMethod.GET })
	public String sendVcode(String phone) {
		int vcode = NumberUtil.getRandomNumber(1000, 9999);
		redisCache.set(Constant.CACHE_PHONE_VCODE_LOGIN_HM+phone, vcode);
		try {
			MsgHelp.sendCheckCode(phone, vcode+"",Constant.USER_TYPE_HOTELMANAGER);
		} catch (ClientException e) {
			e.printStackTrace();
			return new ResponseWrapTo(CodeType.fail,"验证码发送异常",null).toJson();
		}
		return new ResponseWrapTo(CodeType.success,"验证码发送成功",null).toJson();
	}
	
	
	/**
	 * 
		 * @description	获取验证码
		 * @author  han1088
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getPasswordVC", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String getPasswordVC(){
		String phone = "";
		Hotelmanager hm = (Hotelmanager) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
		if(hm == null){
			return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
		}
		phone = hm.getTel();
		
		//将用户钱包存进session
		RequestUtil.setSessionAttribute(Constant.SESSION_LOGIN_USER, hm);
		int vcode = NumberUtil.getRandomNumber(1000, 9999);
		redisCache.set(Constant.CACHE_PHONE_VCODE_HM_PASSWORD+phone, vcode);
		try {
			MsgHelp.sendCheckCode(phone, vcode+"",Constant.USER_TYPE_HOTELMANAGER);
		} catch (ClientException e) {
			e.printStackTrace();
			return new ResponseWrapTo(CodeType.fail,"验证码发送异常",null).toJson();
		}
		return new ResponseWrapTo(CodeType.success,"验证码发送成功",null).toJson();
		
	}

	/**
	 * 
		 * @description	验证码验证
		 * @author  han1088
		 * @param phone
		 * @param vcode
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "verifyVCode", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String verifyVCode(String vcode){
		String phone = "";
		Hotelmanager hm = (Hotelmanager) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
		if(hm == null){
			return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
		}
		phone = hm.getTel();
		
		String cacheVCode = redisCache.get(Constant.CACHE_PHONE_VCODE_HM_PASSWORD+phone);
		if(cacheVCode == null){
			//返回值固定-3
			return new ResponseWrapTo(-3, "验证码失效请重新获取", null).toJson();
		}
			//返回值固定-3
		if(!cacheVCode.equals(vcode)){
			RequestUtil.setSessionAttribute("verifyVCode", Constant.VERIFYVCODE_NOPASS);
			return new ResponseWrapTo(-3, "验证码错误", null).toJson();
		}
		RequestUtil.setSessionAttribute("verifyVCode", Constant.VERIFYVCODE_PASS);
		redisCache.delete(Constant.CACHE_PHONE_VCODE_HM_PASSWORD+phone);
		return new ResponseWrapTo(CodeType.success, "验证成功", null).toJson();
	}

	/**
	 * 
		 * @description	设置密码
		 * @author  han1088
		 * @param password
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "setPassword", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String setPassword(String password){
		Integer result = (Integer) RequestUtil.getSessionAttribute("verifyVCode");
		Hotelmanager hm = (Hotelmanager) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
		//已经验证过
		if(result.intValue() == Constant.VERIFYVCODE_PASS.intValue()){
			hm.setPassword(MD5.toMD5(password));
			hotelmanagerService.update(hm);
			return new ResponseWrapTo(CodeType.success, "密码设置成功", null).toJson();
		}
		return new ResponseWrapTo(CodeType.fail, "验证码错误", null).toJson();
	}
	
	@ResponseBody
	@RequestMapping(value = "logout", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String logout(HttpServletRequest request){
		HttpSession session = RequestUtil.getSession();
		if (session != null) {
			Enumeration<String> keys = RequestUtil.getSession().getAttributeNames();
			while (keys.hasMoreElements()) {
				String key = (String) keys.nextElement();
				RequestUtil.getSession().removeAttribute(key);
			}
			Cookie[] cookies = request.getCookies();
			for (Cookie cookie : cookies) {
				cookie.setMaxAge(0);
			}
		}
		return new ResponseWrapTo(CodeType.success, "成功退出", null).toJson();
	}
	
	/**
	 * 
		 * @description	用于手机号+密码登陆
		 * @author  han1088
		 * @param phone 手机号
		 * @param code 验证码
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "loginByPassword", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET })
	public String loginByPassword(HttpServletRequest req,String phone, String password) {
		Map result = hotelmanagerService.loginByPassword(phone, password);
		int code = (int) result.get("code");
		String message = (String) result.get("message");
		Hotelmanager hotelmanager = null;
		if(code == 0){//登陆成功
			hotelmanager = (Hotelmanager) result.get("user");
			Hotel h = hotelmanager.getHotel();
			if(h == null){
				hotelmanager.setState(Constant.HOTEL_STATE_WAITAUTH);
			}else{
				hotelmanager.setState(hotelmanager.getHotel().getState());
			}
			String token = hotelmanager.getUserid()+"_"+MD5.toMD5(UUIDUtils.getUUID()+"");
			System.out.println(token);
			//给一个默认的酒店
			List<HotelHmanager> re = hhmService.getBindHotelManager(null, hotelmanager.getUserid(), 1);
			if(hotelmanager.getHotelid() == null){//此前未绑定过酒店
				if(re.size()>0){
					hotelmanager.setHotelid(re.get(0).getHotelid());
				}
			}
			
			redisCache.set(Constant.SESSION_LOGIN_USER_TOKEN+hotelmanager.getUserid(), token);
			RequestUtil.setSessionAttribute(Constant.SESSION_LOGIN_USER, hotelmanager);
			hotelmanager.setToken(token);
			hotelmanager.setLastlogintime(new Date());
			hotelmanagerService.update(hotelmanager);
			JSONObject js = (JSONObject) JSONObject.toJSON(hotelmanager);
			js.put("hotelmanager", hotelmanager);
			return new ResponseWrapTo(CodeType.success,"登陆成功",js).toJson();
		}
		return new ResponseWrapTo(CodeType.fail,message,null).toJson();
	}
	
}
